# Copyright 2019 Open Source Robotics Foundation, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import contextlib
import os
import re
import sys
import unittest

from launch import LaunchDescription
from launch.actions import ExecuteProcess
from launch.actions import RegisterEventHandler
from launch.actions import ResetEnvironment
from launch.actions import SetEnvironmentVariable
from launch.event_handlers import OnShutdown

import launch_testing
import launch_testing.actions
import launch_testing.asserts
import launch_testing.markers
import launch_testing.tools
from launch_testing_ros.actions import EnableRmwIsolation
import launch_testing_ros.tools

import pytest

from rclpy.utilities import get_available_rmw_implementations
from ros2cli.helpers import get_rmw_additional_env

import yaml


# Skip cli tests on Windows while they exhibit pathological behavior
# https://github.com/ros2/build_farmer/issues/248
if sys.platform.startswith('win'):
    pytest.skip(
            'CLI tests can block for a pathological amount of time on Windows.',
            allow_module_level=True)


@pytest.mark.rostest
@launch_testing.parametrize('rmw_implementation', get_available_rmw_implementations())
def generate_test_description(rmw_implementation):
    path_to_action_server_executable = os.path.join(
        os.path.dirname(__file__), 'fixtures', 'fibonacci_action_server.py'
    )
    additional_env = get_rmw_additional_env(rmw_implementation)
    set_env_actions = [SetEnvironmentVariable(k, v) for k, v in additional_env.items()]
    return LaunchDescription([
        # Always restart daemon to isolate tests.
        ExecuteProcess(
            cmd=['ros2', 'daemon', 'stop'],
            name='daemon-stop',
            on_exit=[
                *set_env_actions,
                EnableRmwIsolation(),
                RegisterEventHandler(OnShutdown(on_shutdown=[
                    # Stop daemon in isolated environment with proper ROS_DOMAIN_ID
                    ExecuteProcess(
                        cmd=['ros2', 'daemon', 'stop'],
                        name='daemon-stop-isolated',
                        # Use the same isolated environment
                        additional_env=dict(additional_env),
                    ),
                    # This must be done after stopping the daemon in the isolated environment
                    ResetEnvironment(),
                ])),
                ExecuteProcess(
                    cmd=['ros2', 'daemon', 'start'],
                    name='daemon-start',
                    on_exit=[
                        ExecuteProcess(
                            cmd=[sys.executable, path_to_action_server_executable],
                        ),
                        launch_testing.actions.ReadyToTest()
                    ],
                )
            ]
        ),
    ])


def get_fibonacci_send_goal_output(*, order=1, with_feedback=0):
    assert order > 0
    output = [
        'Waiting for an action server to become available...',
        'Sending goal:',
        '     order: {}'.format(order),
        '',
        re.compile('Goal accepted with ID: [a-f0-9]+'),
        '',
    ]
    sequence = [0, 1]
    for _ in range(order - 1):
        sequence.append(sequence[-1] + sequence[-2])
        if with_feedback > 0:
            output.append('Feedback:')
            output.extend(('    ' + yaml.dump({
                'sequence': sequence
            })).splitlines())
            output.append('')
            with_feedback -= 1
    output.append('Result:'),
    output.extend(('    ' + yaml.dump({
        'sequence': sequence
    })).splitlines())
    output.append('')
    output.append('Goal finished with status: SUCCEEDED')
    return output


class TestROS2ActionCLI(unittest.TestCase):

    @classmethod
    def setUpClass(
        cls,
        launch_service,
        proc_info,
        proc_output,
        rmw_implementation
    ):
        @contextlib.contextmanager
        def launch_action_command(self, arguments):
            additional_env = {
                'PYTHONUNBUFFERED': '1',
            }

            action_command_action = ExecuteProcess(
                cmd=['ros2', 'action', *arguments],
                name='ros2action-cli', output='screen',
                additional_env=additional_env
            )
            with launch_testing.tools.launch_process(
                launch_service, action_command_action, proc_info, proc_output,
                output_filter=launch_testing_ros.tools.basic_output_filter(
                    filtered_rmw_implementation=rmw_implementation
                )
            ) as action_command:
                yield action_command
        cls.launch_action_command = launch_action_command

    def test_info_on_nonexistent_action(self):
        with self.launch_action_command(arguments=['info', '/not_an_action']) as action_command:
            assert action_command.wait_for_shutdown(timeout=10)
        assert action_command.exit_code == launch_testing.asserts.EXIT_OK
        assert launch_testing.tools.expect_output(
            expected_lines=[
                'Action: /not_an_action',
                'Action clients: 0',
                'Action servers: 0',
            ],
            text=action_command.output,
            strict=False
        )

    @launch_testing.markers.retry_on_failure(times=5, delay=1)
    def test_fibonacci_info(self):
        with self.launch_action_command(arguments=['info', '/test/fibonacci']) as action_command:
            assert action_command.wait_for_shutdown(timeout=10)
        assert action_command.exit_code == launch_testing.asserts.EXIT_OK
        assert launch_testing.tools.expect_output(
            expected_lines=[
                'Action: /test/fibonacci',
                'Action clients: 0',
                'Action servers: 1',
                '  /test/fibonacci_action_server'
            ],
            text=action_command.output,
            strict=False
        )

    @launch_testing.markers.retry_on_failure(times=5, delay=1)
    def test_fibonacci_info_with_types(self):
        with self.launch_action_command(
                arguments=['info', '-t', '/test/fibonacci']) as action_command:
            assert action_command.wait_for_shutdown(timeout=10)
        assert action_command.exit_code == launch_testing.asserts.EXIT_OK
        assert launch_testing.tools.expect_output(
            expected_lines=[
                'Action: /test/fibonacci',
                'Action clients: 0',
                'Action servers: 1',
                '  /test/fibonacci_action_server [test_msgs/action/Fibonacci]'
            ],
            text=action_command.output,
            strict=False
        )

    @launch_testing.markers.retry_on_failure(times=5, delay=1)
    def test_fibonacci_info_count(self):
        with self.launch_action_command(
                arguments=['info', '-c', '/test/fibonacci']) as action_command:
            assert action_command.wait_for_shutdown(timeout=10)
        assert action_command.exit_code == launch_testing.asserts.EXIT_OK
        assert launch_testing.tools.expect_output(
            expected_lines=[
                'Action: /test/fibonacci',
                'Action clients: 0',
                'Action servers: 1',
            ],
            text=action_command.output,
            strict=False
        )

    @launch_testing.markers.retry_on_failure(times=5, delay=1)
    def test_list(self):
        with self.launch_action_command(arguments=['list']) as action_command:
            assert action_command.wait_for_shutdown(timeout=10)
        assert action_command.exit_code == launch_testing.asserts.EXIT_OK
        assert launch_testing.tools.expect_output(
            expected_lines=['/test/fibonacci'],
            text=action_command.output,
            strict=True
        )

    @launch_testing.markers.retry_on_failure(times=5, delay=1)
    def test_list_with_types(self):
        with self.launch_action_command(arguments=['list', '-t']) as action_command:
            assert action_command.wait_for_shutdown(timeout=10)
        assert action_command.exit_code == launch_testing.asserts.EXIT_OK
        assert launch_testing.tools.expect_output(
            expected_lines=['/test/fibonacci [test_msgs/action/Fibonacci]'],
            text=action_command.output, strict=True
        )

    @launch_testing.markers.retry_on_failure(times=5, delay=1)
    def test_list_count(self):
        with self.launch_action_command(arguments=['list', '-c']) as action_command:
            assert action_command.wait_for_shutdown(timeout=10)
        assert action_command.exit_code == launch_testing.asserts.EXIT_OK
        command_output_lines = action_command.output.splitlines()
        assert len(command_output_lines) == 1
        assert int(command_output_lines[0]) == 1

    @launch_testing.markers.retry_on_failure(times=5, delay=1)
    def test_type(self):
        with self.launch_action_command(arguments=['type', '/test/fibonacci']) as action_command:
            assert action_command.wait_for_shutdown(timeout=10)
        assert action_command.exit_code == launch_testing.asserts.EXIT_OK
        assert launch_testing.tools.expect_output(
            expected_lines=['test_msgs/action/Fibonacci'],
            text=action_command.output, strict=True
        )

    @launch_testing.markers.retry_on_failure(times=5, delay=1)
    def test_find(self):
        with self.launch_action_command(
                arguments=['find', 'test_msgs/action/Fibonacci']) as action_command:
            assert action_command.wait_for_shutdown(timeout=10)
        assert action_command.exit_code == launch_testing.asserts.EXIT_OK
        assert launch_testing.tools.expect_output(
            expected_lines=['/test/fibonacci'],
            text=action_command.output, strict=True
        )

    @launch_testing.markers.retry_on_failure(times=5, delay=1)
    def test_find_count(self):
        with self.launch_action_command(
                arguments=['find', '-c', 'test_msgs/action/Fibonacci']) as action_command:
            assert action_command.wait_for_shutdown(timeout=10)
        assert action_command.exit_code == launch_testing.asserts.EXIT_OK
        command_output_lines = action_command.output.splitlines()
        assert len(command_output_lines) == 1
        assert int(command_output_lines[0]) == 1

    @launch_testing.markers.retry_on_failure(times=5, delay=1)
    def test_send_fibonacci_goal_timeout_server_not_available(self):
        with self.launch_action_command(
            arguments=[
                'send_goal',
                '-t', '1',
                '/test/fibonacci_noexist',
                'test_msgs/action/Fibonacci',
                '{order: 1}'
            ],
        ) as action_command:
            assert action_command.wait_for_shutdown(timeout=10)
        assert action_command.exit_code == launch_testing.asserts.EXIT_OK
        assert launch_testing.tools.expect_output(
            expected_lines=[
                'Waiting for an action server to become available...',
                'Action server is not available after waiting 1 seconds.'
            ],
            text=action_command.output, strict=False
        )

    @launch_testing.markers.retry_on_failure(times=5, delay=1)
    def test_send_fibonacci_goal(self):
        with self.launch_action_command(
            arguments=[
                'send_goal',
                '/test/fibonacci',
                'test_msgs/action/Fibonacci',
                '{order: 5}'
            ],
        ) as action_command:
            assert action_command.wait_for_shutdown(timeout=10)
        assert action_command.exit_code == launch_testing.asserts.EXIT_OK
        assert launch_testing.tools.expect_output(
            expected_lines=get_fibonacci_send_goal_output(order=5),
            text=action_command.output, strict=False
        )

    @launch_testing.markers.retry_on_failure(times=5, delay=1)
    def test_send_fibonacci_goal_with_feedback(self):
        with self.launch_action_command(
            arguments=[
                'send_goal',
                '-f',
                '/test/fibonacci',
                'test_msgs/action/Fibonacci',
                '{order: 5}'
            ],
        ) as action_command:
            assert action_command.wait_for_shutdown(timeout=10)
        assert action_command.exit_code == launch_testing.asserts.EXIT_OK
        assert launch_testing.tools.expect_output(
            expected_lines=get_fibonacci_send_goal_output(order=5, with_feedback=1),
            text=action_command.output, strict=False
        )
